/********************************************************************************
*Function：float temperature()
*Description：实时温度的读取函数
*Input：
*Output：tempture
*Return：int
********************************************************************************/
#include"temperature.h"

float get_temp()
{	
	int fd;
	unsigned short temp[7];
	unsigned short cptemp = 0;
	float tempture;
	int i,j;

	//打开温度传感器设备，循环读取温度值取中值
	if ((fd=open("/dev/DS18B20",O_RDWR)) < 0)
	{
		perror("Open Device DS18B20.\r\n");
 		return OPEN_DEV_FAILURE;
	}
	else
	{ 
		for(i = 0; i < 7; i++)
		{
			read(fd, &temp[i], sizeof(unsigned short));			
		}
		for(i = 0; i < 6;i++)
		{
			for(j = i + 1; j < 7; j++)
			{
				if(temp[j]<temp[i])
				{
					cptemp = temp[j];
					temp[j] = temp[i];
					temp[i] = cptemp;
				}
			}
		}
		if (temp[3] & 0xf800)
		{
			temp[3] = ((~temp[3]) + 1);
			tempture = -(((float) temp[3])/16 );
		}
		else
		{
			tempture = ((float) temp[3])/16;
		}
		
	}      

	close(fd);

	return tempture;    
      
}





